package com.jiumi.order.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiumi.common.utils.DateUtils;
import com.jiumi.common.utils.StringUtils;
import com.jiumi.order.domain.BaseOrderDetail;
import com.jiumi.order.mapper.BaseOrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiumi.order.mapper.BaseOrderPayInfoMapper;
import com.jiumi.order.domain.BaseOrderPayInfo;
import com.jiumi.order.service.IBaseOrderPayInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收款管理Service业务层处理
 *
 * @author jiumi
 * @date 2023-06-02
 */
@Service
class BaseOrderPayInfoServiceImpl extends ServiceImpl<BaseOrderPayInfoMapper,BaseOrderPayInfo>  implements IBaseOrderPayInfoService
{
    @Autowired
    private BaseOrderPayInfoMapper baseOrderPayInfoMapper;

    @Autowired
    private BaseOrderDetailMapper baseOrderDetailMapper;

    /**
     * 查询收款管理
     *
     * @param id 收款管理主键
     * @return 收款管理
     */
    @Override
    public BaseOrderPayInfo selectBaseOrderPayInfoById(Long id)
    {
        BaseOrderPayInfo payInfo= baseOrderPayInfoMapper.selectBaseOrderPayInfoById(id);
        List<BaseOrderDetail> detailList= baseOrderDetailMapper.selectOrderDetailListByids(payInfo.getOrderDetailId());
        String info="";
        for(BaseOrderDetail detail :detailList){
            if(StringUtils.isEmpty(info)){
                info=detail.getCourseName();
            }else{
                info=info+","+detail.getCourseName();
            }
        }
        payInfo.setOrderDetailInfo(info);
        return payInfo;
    }

    /**
     * 查询收款管理列表
     *
     * @param baseOrderPayInfo 收款管理
     * @return 收款管理
     */
    @Override
    public List<BaseOrderPayInfo> selectBaseOrderPayInfoList(BaseOrderPayInfo baseOrderPayInfo)
    {
        List<BaseOrderPayInfo> payInfoList= baseOrderPayInfoMapper.selectBaseOrderPayInfoList(baseOrderPayInfo);
        payInfoList.stream().forEach(payInfo->{
            List<BaseOrderDetail> detailList= baseOrderDetailMapper.selectOrderDetailListByids(payInfo.getOrderDetailId());
            String info="";
            for(BaseOrderDetail detail :detailList){
                if(StringUtils.isEmpty(info)){
                    info=detail.getCourseName();
                }else{
                    info=info+","+detail.getCourseName();
                }
            }
            payInfo.setOrderDetailInfo(info);
        });
        return payInfoList;
    }

    /**
     * 新增收款管理
     *
     * @param baseOrderPayInfo 收款管理
     * @return 结果
     */
    @Override
    public int insertBaseOrderPayInfo(BaseOrderPayInfo baseOrderPayInfo)
    {
        baseOrderPayInfo.setCreateTime(DateUtils.getNowDate());
        return baseOrderPayInfoMapper.insertBaseOrderPayInfo(baseOrderPayInfo);
    }

    /**
     * 修改收款管理
     *
     * @param baseOrderPayInfo 收款管理
     * @return 结果
     */
    @Override
    public int updateBaseOrderPayInfo(BaseOrderPayInfo baseOrderPayInfo)
    {
        baseOrderPayInfo.setUpdateTime(DateUtils.getNowDate());
        return baseOrderPayInfoMapper.updateBaseOrderPayInfo(baseOrderPayInfo);
    }

    /**
     * 批量删除收款管理
     *
     * @param ids 需要删除的收款管理主键
     * @return 结果
     */
    @Override
    public int deleteBaseOrderPayInfoByIds(Long[] ids)
    {
        return baseOrderPayInfoMapper.deleteBaseOrderPayInfoByIds(ids);
    }

    /**
     * 删除收款管理信息
     *
     * @param id 收款管理主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBaseOrderPayInfoById(Long id)
    {
        int resultNum= baseOrderPayInfoMapper.deleteBaseOrderPayInfoById(id);
        if(resultNum>0){
            baseOrderDetailMapper.updateOrderDetailPid(id);
        }
        return resultNum;
    }

    @Override
    public int cancelBaseOrderPayInfoById(BaseOrderPayInfo payInfo) {
        BaseOrderPayInfo editInfo=new BaseOrderPayInfo();
        editInfo.setId(payInfo.getId());
        editInfo.setPaymentStatus("2");
        editInfo.setUpdateTime(DateUtils.getNowDate());
        editInfo.setUpdateBy(payInfo.getUpdateBy());
        int resultNum=baseOrderPayInfoMapper.updateBaseOrderPayInfo(editInfo);
        if(resultNum>0){
            baseOrderDetailMapper.updateOrderDetailPid(payInfo.getId());
        }
        return resultNum;
    }
}
